//
//  XYDiscoverTextFiled.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/26.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit

class XYDiscoverTextFiled: UIView {
    
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var toRight: NSLayoutConstraint!
    
    @IBOutlet weak var textField: UITextField!
    
    @IBAction func didCickCancel(sender: AnyObject) {
        
        textField.endEditing(true)
        toRight.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.textField.layoutIfNeeded()
        }
        
    }
    @IBAction func didBeginEdit(sender: AnyObject) {
        
        
        // 执行约束动画的步骤 
        // 1.改约束
        // 2.调用 该控件的layoutIfNeeded
        toRight.constant = cancelBtn.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.textField.layoutIfNeeded()
        }
        
        
    }
    
    override func awakeFromNib() {
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.contentMode = .Center
        leftView.frame.size = CGSizeMake(frame.height, frame.height)
        textField.leftView = leftView
        textField.leftViewMode = .Always
        
    }
    
    class func discoverTextField() -> XYDiscoverTextFiled{
        
        return NSBundle.mainBundle().loadNibNamed("XYDiscoverTextFiled", owner: nil, options: nil).last as! XYDiscoverTextFiled
    }

}
